package com.tonyodev.fetch2core;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Objects;

/**
 * 请求类
 *
 * @since 2021-05-20
 */
public final class FileResource implements Sequenceable, Serializable {
    /**
     * The File Path. Can be a file or uri
     */
    private String file = "";

    /**
     * Unique Short name of the File Resource.
     * Clients can request data from the server using the file resource name
     * example Url: fetchlocal://127.0.0.1/:7428/text.txt
     */
    private String name = "";
    /**
     * Set or get the extras for this file resource. Use this to
     * save and get custom key/value data for the file resource.
     */
    private Extras extras = Extras.getEmptyExtras();
    /**
     * Unique File Resource Identifier.
     * Clients can request data from the server using the file resource id
     * example Url: fetchlocal://127.0.0.1:7428/84562
     */
    private long id = 0L;
    /**
     * Content Length
     */
    private long length = 0L;
    /**
     * The File Resource md5 checksum string. If Empty Fetch File Server will generate the md5
     * checksum when added to a file server instance.
     * This is sent in the server response to the client.
     */
    private String md5;

    /**
     * 构造函数
     */
    public FileResource() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getLength() {
        return length;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Extras getExtras() {
        return extras;
    }

    public void setExtras(Extras value) {
        this.extras = value;
    }

    public String getMd5() {
        return md5;
    }

    public void setMd5(String md5) {
        this.md5 = md5;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        FileResource that = (FileResource) o;
        return id == that.id &&
            length == that.length &&
            Objects.equals(file, that.file) &&
            Objects.equals(name, that.name) &&
            Objects.equals(extras, that.extras) &&
            Objects.equals(md5, that.md5);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, length, file, name, extras, md5);
    }

    @Override
    public String toString() {
        return "FileResource(id=" + this.id + ", length=" + this.length + ", file='" + this.file + "'," + " name='" + this.name + "', extras='" + this.extras + "', md5='" + this.md5 + "')";
    }

    @Override
    public boolean marshalling(Parcel parcel) {
        parcel.writeLong(id);
        parcel.writeString(name);
        parcel.writeLong(length);
        parcel.writeString(file);
        parcel.writeSerializable(new HashMap(extras.getMap()));
        parcel.writeString(md5);
        return true;
    }

    @Override
    public boolean unmarshalling(Parcel parcel) {
        id = parcel.readLong();
        name = parcel.readString();
        length = parcel.readLong();
        file = parcel.readString();
        md5 = parcel.readString();
        extras = parcel.readSerializable(Extras.class);
        return false;
    }

    /**
     * 序列化
     */
    public static final Sequenceable.Producer<FileResource>
        PRODUCER = new Sequenceable.Producer() {
        /**
         * 从序列化创建新对象
         *
         * @param in 序列化
         * @return FileResource
         */
        public FileResource createFromParcel(Parcel in) {
            // Initialize an instance first, then do customized unmarshlling.
            FileResource instance = new FileResource();
            instance.unmarshalling(in);
            return instance;
        }
    };
}
